import ComponentBase from "../engine/ComponentBase";
import { Logger } from "../engine/Logger";
import UIManager from "../engine/UIManager";

import { GameConfig } from "../game/config/GameConfig";
import { RoomConfig } from "../game/config/RoomConfig";
import { ViewConfig } from "../game/config/ViewConfig";

import { RoomModel } from "../game/model/RoomModel";
import { UIHelper } from "../game/util/UIHelper";
import GameController from "../GameController";


const { ccclass, property } = cc._decorator;

@ccclass
export default class VideoControlPrefab extends ComponentBase {

    public static instance: VideoControlPrefab;
    private videoUI: cc.Node
    private actionIndex: number = -1
    private timeCount: number = 0
    private video_play: cc.Node
    private video_pause: cc.Node;

    private isPause: boolean = false
    // private time_inteval: number = cc.game.getFrameRate() ;
    private time_inteval: number = cc.game.getFrameRate();
    private speed: number = 1
    private txtSpeed: cc.Label
    private videoBg: cc.Node
    private isActive: boolean = false

    onEnable(): void {
        super.onEnable();
        this.speed = 1
        let videBg: cc.Node = this.findChild(this.node, "videBg")
        videBg.active = RoomModel.instance.isPlayVideo
        this.videoBg = videBg
        this.actionIndex = -1;
        VideoControlPrefab.instance = this
        this.videoUI = this.findChild(this.node, "videoUI")
        this.videoUI.active = RoomModel.instance.isPlayVideo

        let video_back: cc.Node = this.findChild(this.videoUI, "video_back")
        UIHelper.onAddEvent(video_back, () => {
            GameController.instance.leaveVideo()
            // this.actionIndex = RoomModel.instance.videoInfo.actionList.length;
            // this.nextAction()
        })
        let video_pre: cc.Node = this.findChild(this.videoUI, "video_pre")
        UIHelper.onAddEvent(video_pre, () => {
            if (this.actionIndex >= 0) {
                this.preAction()
                this.actionIndex--;
                this.timeCount = 0
                // this.playAction()
            }
        })
        this.video_play = this.findChild(this.videoUI, "video_play")
        this.video_pause = this.findChild(this.videoUI, "video_pause")
        UIHelper.onAddEvent(this.video_pause, () => {
            this.isPause = true
        })
        UIHelper.onAddEvent(this.video_play, () => {
            this.isPause = false
        })
        let video_next: cc.Node = this.findChild(this.videoUI, "video_next")
        UIHelper.onAddEvent(video_next, () => {
            // if (this.speed > 1) {
            //     this.speed = 1
            // } else {
            //     this.speed = 2
            // }
            this.nextAction()
        })
        let video_quick: cc.Node = this.findChild(this.videoUI, "video_quick")
        if (video_quick) {
            this.txtSpeed = this.getChild(video_quick, "txtSpeed", cc.Label)
            this.txtSpeed.node.active = true
            UIHelper.onAddEvent(video_quick, () => {
                if (this.speed > 1) {
                    this.speed = 1
                } else {
                    this.speed = 2
                }
            })
        }
        Logger.log("VideoControlPrefab.onEnable", this.videoUI.active, RoomModel.instance.isPlayVideo)
        if (RoomModel.instance.isPlayVideo) {
            this.timeCount = 0
            this.scheduleOnce(()=>{
                this.nextAction()
            }, 0.5)
        }
        this.isActive = RoomModel.instance.isPlayVideo
    }


    protected update(dt: number): void {
        // Logger.log("isActive=", this.isActive, RoomModel.instance.isPlayVideo)
        this.videoUI.active = RoomModel.instance.isPlayVideo
        this.videoBg.active = RoomModel.instance.isPlayVideo
        if (RoomModel.instance.isPlayVideo) {
            this.video_play.active = this.isPause
            this.video_pause.active = !this.isPause;
            if (this.txtSpeed) {
                if (this.speed > 1) {
                    this.txtSpeed.string = "X" + this.speed
                } else {
                    this.txtSpeed.string = "X" + this.speed
                }
            }
            if (!this.isPause) {
                this.timeCount++;
                if (this.timeCount >= Math.floor(this.time_inteval / this.speed)) {
                    this.nextAction()
                }
            }
        }
    }

    private nextAction() {
        this.timeCount = 0
        this.actionIndex++
        let isEnd: boolean = false
        if (RoomModel.instance.videoInfo && RoomModel.instance.videoInfo.actionList) {
            // Logger.log("nextAction=", RoomModel.instance.videoInfo, )
            if (this.actionIndex < RoomModel.instance.videoInfo.actionList.length) {
                this.playAction()
            } else {
                isEnd = true
         
            }
        }
        else {
            isEnd = true
            GameController.instance.leaveVideo()
        }
    }

    private preAction() {

    }


    private playAction() {
        // Logger.log("playAction==", this.actionIndex,RoomModel.instance.videoInfo )

    }

    onDisable(): void {
        super.onDisable()
        this.actionIndex = -1
        this.timeCount = 0
        this.isActive = false
        Logger.log("VideoControlPrefab onDisable===")
    }

}

